package org.tloss.web.editor.popup.actions;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.tloss.web.editor.GeneratorHelper;
import org.tloss.web.editor.Utils;
import org.tloss.web.editor.utils.Constants;
import org.tloss.web.editor.utils.Generator;

public class GenerateSqlAction implements IObjectActionDelegate {

	private Shell shell;

	/**
	 * Constructor for Action1.
	 */
	public GenerateSqlAction() {
		super();
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}

	GeneratorHelper generatorHelper = new GeneratorHelper();

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
		final IProject project = Utils.getCurrentProject();
		Job job = new Job("Generate Sql") {
			protected IStatus run(IProgressMonitor monitor) {
				generatorHelper.setProject(project);
				IResource container = project.findMember(Constants.SQL_FOLDER);
				Generator.generateSQL(project, container, monitor,
						generatorHelper);
				return Status.OK_STATUS;
			}

		};
		job.setPriority(Job.LONG);
		job.schedule();
	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
	}

}
